<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Search extends CI_Controller {
	private $user_code = '';
	public function __construct()
	{
		parent::__construct();
		$this->lang->load('i18n');
		$this->load->helper('url');
		$this->load->config('config');
		$this->load->library('table');
		$this->load->library('pagination');
		
		$loggedIn = $this->session->userdata('loggedIn');
		if(empty($loggedIn) && $this->router->fetch_method() != 'error'){
			$this->session->set_flashdata("errors", array($this->lang->line('lang.search.logginRequired')));
			redirect($this->lang->languageCode().'/search/error', 'location');
		}
		$this->user_code = $this->session->userdata('user')->USERNAME;
	}
	
	public function error(){
		$data = array();
		$data['content'] = $this->load->view('error-content.phtml', null, true);
		$this->load->view('mainpage.phtml', $data);
	}
	
	public function index()
	{
		$searchResult = array();
		if(isset($_GET['trucknumber'])){
			$truckno = $_GET['trucknumber'];
			
			$config = $this->config->item('pagination');
			
			$config['base_url'] = site_url($this->lang->languageCode().'/search/index');
			$config['suffix'] = '?'.http_build_query($_GET, '', "&");
			// we are searching dulieucanxe.
			$this->db->from('dulieucanxe')->like('truck_no', $truckno);
			if($this->session->userdata('role') != ROLE_ADMIN){
				$this->db->where('cust_code', $this->user_code);
			}
			
			$config['total_rows'] = $this->db->get()->num_rows();
			$config['first_link'] = $this->lang->line('lang.search.firstlink');
			$config['last_link'] = $this->lang->line('lang.search.lastlink');
			
			$this->pagination->initialize($config);
			
			$searchResult['paginator'] = $this->pagination->create_links();
			
			// Set table header to show on screen.			
			$this->prepareTableStructure();
			// Data
			$this->db->select($this->config->item('allowed_shown_fields'))->like('truck_no', $truckno);
			if($this->session->userdata('role') != ROLE_ADMIN){
				$this->db->where('cust_code', $this->user_code);
			}
			
			// Calculate the offset which will be used to search.
			$cur_page = $this->uri->segment(4);	
			if(empty($cur_page) == false){
				$offset = ($cur_page - 1) * $config['per_page'];
			} else {
				$offset = 0;
			}		
						
			$searchResult['records'] = $this->db->get('dulieucanxe', $config['per_page'], $offset);
			$searchResult['count'] = $config['total_rows'];
			$searchResult['trucknumber'] = $truckno;
		}
		
		$data = array();
		$data['content'] = $this->load->view('search-content.phtml', $searchResult, true);
		$data['search_selected_class'] = "current";
		$this->load->view('mainpage.phtml', $data);
	}
	
	protected function prepareTableStructure(){
		// Header
		$header_title = array();
		foreach($this->config->item('allowed_header_fields') as $key){
			array_push($header_title, $this->lang->line($key));
		}
		$this->table->set_heading($header_title);
		$this->table->set_heading_classes($this->config->item('header_classes'));
		// Table structure
		$tmpl = array (
				'table_open'          => '<table width="100%" class="search-result-table" cellpadding="4" cellspacing="0">',
				'heading_row_start'   => "<tr class='search-result-header-row'>",
				'heading_row_end'     => '</tr>',
				'heading_cell_start'  => "<th class='search-result-header-cell'>",
				'heading_cell_end'    => '</th>',
				'table_close'         => '</table>'
		);
		
		$this->table->set_template($tmpl);
	}
}

/* End of file import.php */
/* Location: ./application/controllers/import.php */